//main.cpp
#include <iostream>
#include <time.h>
#include <wtypes.h>
using namespace std;

typedef struct _NODE
{
	int data;
	struct _NODE* next;
}NODE;

typedef struct _HASH_TABLE
{
	NODE* value[10];
}HASH_TABLE;

HASH_TABLE* create_hash_table()
{
	HASH_TABLE* pHashTbl = (HASH_TABLE*)malloc(sizeof(HASH_TABLE));
	memset(pHashTbl, 0, sizeof(HASH_TABLE));
	return pHashTbl;
}

NODE* find_data_in_hash(HASH_TABLE* pHashTbl, int data)
{
	NODE* pNode;
	if (NULL == pHashTbl)
	{
		return NULL;
	}

	if (NULL == (pNode = pHashTbl->value[data%10]))
	{
		return NULL;
	}

	while (pNode)
	{
		if (data == pNode->data)
		{
			return pNode;
		}
		pNode = pNode->next;
	}

	return NULL;
}

bool insert_data_into_hash(HASH_TABLE* pHashTbl, int data)
{
	NODE* pNode;
	if (NULL == pHashTbl)
	{
		return false;
	}

	if (NULL == pHashTbl->value[data%10])
	{
		pNode = (NODE*)malloc(sizeof(NODE));
		memset(pNode, 0, sizeof(NODE));
		pNode->data = data;
		pHashTbl->value[data%10] = pNode;
		return true;
	}

	if (NULL != find_data_in_hash(pHashTbl, data))
	{
		return false;
	}

	pNode = pHashTbl->value[data%10];
	while(NULL != pNode->next)
	{
		pNode = pNode->next;
	}

	pNode->next = (NODE*)malloc(sizeof(NODE));
	memset(pNode->next, 0, sizeof(NODE));
	pNode->next->data = data;
	return true;
}

bool delete_data_from_hash(HASH_TABLE* pHashTbl, int data)
{
	NODE* pHead;
	NODE* pNode;
	if (NULL == pHashTbl || NULL == pHashTbl->value[data%10])
	{
		return false;
	}

	if (NULL == (pNode = find_data_in_hash(pHashTbl, data)))
	{
		return false;
	}

	if (pNode == pHashTbl->value[data%10])
	{
		pHashTbl->value[data%10] = pNode->next;
		goto final;
	}

	pHead = pHashTbl->value[data%10];
	while(pNode != pHead->next)
	{
		pHead = pHead->next;
	}
	pHead->next = pNode->next;

final:
	free(pNode);
	return true;
}

int main()
{
	HASH_TABLE* p = create_hash_table();

	//generate random num
	srand((unsigned int)time(NULL));
	int i = 0;
	while (i++ <= 100)
	{
		int n = rand();
		if (!insert_data_into_hash(p, n))
		{
			cout<<"insert error."<<endl;
			return 1;
		}
	}

	cout<<sizeof(unsigned int)<<"\t"<<sizeof(DWORD)<<endl;

	return 0;
}